<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CF_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('caipu_mdl');
		$this->load->driver('cache', array('adapter' => 'file'));
		$this->cachtime = 86400;
	}

	public function index()
	{
		$this->load->helper('text');
		$this->load->model('caidan_mdl');
		$data['caidan_list'] = $this->caidan_mdl->get(array(), 0, 9)->result_array();
		$data['caipu_new'] = $this->_cache_caipu_new();
		$data['caipu_hot'] = $this->_cache_caipu_hot();
		$data['article_list'] = $this->_cache_article_list();

		$data['shicai_season'] = $this->_cache_shicai_season();

		$data['caipu_zaocan'] = $this->_cache_caipu_zaocan();
		$data['caipu_wucan'] = $this->_cache_caipu_wucan();
		$data['caipu_xiawucha'] = $this->_cache_caipu_xiawucha();
		$data['caipu_wancan'] = $this->_cache_caipu_wancan();
		$data['caipu_yexiao'] = $this->_cache_caipu_yexiao();

		// $data['is_logged_in'] = $this->user_login->is_logged_in();
		// $data['username'] = $this->user_login->get_username();
		// $data['uid'] = $this->user_login->get_user_id();
		// $data['user_avartar'] = $this->user_login->get_avatar_dir($this->user_login->get_user_id()); 
		
		$this->load->view('home',$data);
	}

	public function error_404()
	{
		show_404();
	}


	/**
	 * 首页缓存
	 */
	
	//推荐
	public function _cache_caipu_hot()
	{
		$cachestr = 'index_caipu_hot';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = $this->caipu_mdl->get(NULL,0, 3, 'views DESC')->result_array();
			foreach($data as $item){
				$item['coverImage'] = server_img($item['coverImage']);
				$item['author'] = get_rand_name($item['id']);
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$cache,$this->cachtime);
		}
		return $cache;
	}
	//最新
	public function _cache_caipu_new()
	{
		$cachestr = 'index_caipu_new';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = $this->caipu_mdl->get(NULL, mt_rand(10,99999), 3)->result_array();
			foreach($data as $item){
				$item['coverImage'] = server_img($item['coverImage']);
				$item['author'] = get_rand_name($item['id']);
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$cache,$this->cachtime);
		}
		return $cache;
	}
	//早餐
	public function _cache_caipu_zaocan()
	{
		$cachestr = 'index_caipu_zaocan';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = $this->caipu_mdl->get(array('class_name LIKE'=>'%早餐%','status'=>1), mt_rand(100,3560), 3)->result_array();
			foreach($data as $item){
				$item['coverImage'] = server_img($item['coverImage']);
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$cache,$this->cachtime);
		}
		return $cache;
	}
	//午餐
	public function _cache_caipu_wucan()
	{
		$cachestr = 'index_caipu_wucan';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = $this->caipu_mdl->get(array('class_name LIKE'=>'%下饭菜%','status'=>1), mt_rand(1,5500), 3)->result_array();
			foreach($data as $item){
				$item['coverImage'] = server_img($item['coverImage']);
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$cache,$this->cachtime);
		}
		return $cache;
	}
	//下午茶
	public function _cache_caipu_xiawucha()
	{
		$cachestr = 'index_caipu_xiawucha';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = $this->caipu_mdl->get(array('class_name LIKE'=>'%下午茶%','status'=>1), mt_rand(100,2362), 3)->result_array();
			foreach($data as $item){
				$item['coverImage'] = server_img($item['coverImage']);
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$cache,$this->cachtime);
		}
		return $cache;
	}
	//晚餐
	public function _cache_caipu_wancan()
	{
		$cachestr = 'index_caipu_wancan';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = $this->caipu_mdl->get(array('class_name LIKE'=>'%素菜%','status'=>1), mt_rand(1,490), 3)->result_array();
			foreach($data as $item){
				$item['coverImage'] = server_img($item['coverImage']);
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$cache,$this->cachtime);
		}
		return $cache;
	}
	//夜宵
	public function _cache_caipu_yexiao()
	{
		$cachestr = 'index_caipu_yexiao';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$data = $this->caipu_mdl->get(array('class_name LIKE'=>'%减肥%','status'=>1), mt_rand(1,510), 3)->result_array();
			foreach($data as $item){
				$item['coverImage'] = server_img($item['coverImage']);
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$cache,$this->cachtime);
		}
		return $cache;
	}
	//文章
	public function _cache_article_list()
	{
		$cachestr = 'index_article_list';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$this->load->model('article_mdl');
			$data = $this->article_mdl->get(array('cid !='=>'123'), 0, 9)->result_array();
			foreach($data as $item){
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$data,$this->cachtime);
		}
		return $cache;
	}
	//时令
	public function _cache_shicai_season()
	{
		$cachestr = 'index_shicai_season';
		$cache = $this->cache->get($cachestr);
		if(!$cache){
			$this->load->helper('global');
			$this->load->model('shicai_mdl');
			$data = $this->shicai_mdl->get(array('season'=>get_season()), 0, 8)->result_array();
			foreach($data as $item){
				$cache[] = $item;
			}
			$this->cache->save($cachestr,$data,$this->cachtime);
		}
		return $cache;
	}
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */